//
//  YZMSegmentView.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/23.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMSegmentView.h"

#import <Masonry/Masonry.h>


static const float segment_view_margin_for_red_line = 20;

@interface YZMScrollView: UIScrollView

@end

@implementation YZMScrollView

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint translation = [(UIPanGestureRecognizer *)gestureRecognizer translationInView:self];
        return fabs(translation.x) > fabs(translation.y);
    }
    return YES;
}

@end


@interface YZMSegmentView()<UIScrollViewDelegate>

@end

@implementation YZMSegmentView

@synthesize titleArray = _titleArray;


- (id)init{
    
    return [self initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
}

- (id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createObjects];
        [self addConstraints];
        
        _buttonArray = [NSMutableArray array];
    }
    
    return self;
}


- (NSInteger)currentIndex{
    
    return  (NSInteger)(_contentSV.contentOffset.x/self.frame.size.width);
}

#pragma mark -- Event Response

- (void)clickForSwitchView:(UIButton *)sender{
    
    [self clickAtIndex:sender.tag];
}

#pragma mark  -- Public Method
- (void)clickAtIndex:(NSInteger)index{

//    [_contentSV setContentOffset:CGPointMake(index * self.frame.size.width, 0) animated:NO];

    NSInteger width = self.frame.size.width ? self.frame.size.width : ScreenWidth ;
    [UIView animateWithDuration:0.25 animations:^{
        _contentSV.contentOffset = CGPointMake(index * width, 0);
    }];
    [self refreshContrainsAtButtonIndex:index animation:YES];
}

- (void)noAnimationAtIndex:(NSInteger)index{
     _contentSV.contentOffset = CGPointMake(index * self.frame.size.width, 0);
    [self refreshContrainsAtButtonIndex:index animation:NO];
}

#pragma mark -- UIScrollViewDelegate

/*红色选中条岁视图滚动而移动*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [_redLineView mas_updateConstraints:^(MASConstraintMaker *maker){
        
        maker.left.mas_equalTo(scrollView.contentOffset.x/scrollView.contentSize.width * scrollView.frame.size.width +segment_view_margin_for_red_line/2);
    }];
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = round(scrollView.contentOffset.x / ScreenWidth);
    [self refreshContrainsAtButtonIndex:index animation:YES];
}

///*手指滑动将要停止*/
//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{    //将要停止前的坐标
//
//    if (fabs(_beginOffset.x - targetContentOffset->x) < 0.00001) {
//
//        return;
//    }
//
//    NSInteger index = targetContentOffset->x /scrollView.frame.size.width;
//    [self refreshContrainsAtButtonIndex:index];
//}
//
///*手指滑动将要开始*/
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//
//    _beginOffset = scrollView.contentOffset;
//}


#pragma mark -- Private Method

/*页面切换时，刷新页面*/
- (void)refreshContrainsAtButtonIndex:(NSUInteger)buttonIndex animation:(BOOL)animation{
    /*触发页面切换时，调用代理的方法*/

    if (self.delegate && [self.delegate respondsToSelector:@selector(segmentView:didScrollToIndexView:)]) {
        
        [self.delegate segmentView:self didScrollToIndexView:buttonIndex];
    }
    
    for (UIButton *button in _buttonArray) {
        
        button.selected = NO;
    }
    
    ((UIButton *)_buttonArray[buttonIndex]).selected = YES;
    
    UIView *indexView = _viewArray[buttonIndex];
    [_containerView mas_remakeConstraints:^(MASConstraintMaker *maker){
        
        maker.edges.equalTo(_contentSV);
        maker.width.equalTo(_titleView).multipliedBy(_buttonArray.count);
        if ([NSStringFromClass([self class]) isEqualToString:@"YZMSegmentViewScroll"]) {
            
            maker.height.greaterThanOrEqualTo(indexView);
            maker.height.mas_greaterThanOrEqualTo(80);
            
        }else{
            
            maker.height.equalTo(_contentSV);
        }
        
    }];
    if (animation) {
        [UIView animateWithDuration:0.5f animations:^{
            
            [self layoutIfNeeded];
            
            if (self.superview.superview) {
                
                [self.superview.superview layoutIfNeeded];
            }
        }];
    }
    
}

- (void)createObjects{
    
    _titleView = [UIView new];
    _titleView.backgroundColor = [YZMAppStyle colorForMain];
    [self addSubview:_titleView];
    
    if ([NSStringFromClass([self class]) isEqualToString:@"YZMSegmentViewScroll"]) {
        _contentSV = [YZMScrollView new];
    }else{
        _contentSV = [UIScrollView new];
    }
    _contentSV.backgroundColor = [UIColor clearColor];
    _contentSV.delegate = self;
    _contentSV.pagingEnabled = YES;
    _contentSV.showsHorizontalScrollIndicator = NO;
    _contentSV.bounces = NO;
    [self addSubview:_contentSV];
    
    _containerView = [UIView new];
    _containerView.backgroundColor = [UIColor clearColor];
    [_contentSV addSubview:_containerView];
}

- (void)addConstraints{
    
}

@end
